#include <config.h>

#include <hal_uart.h>
#include <kprintf.h>
#include <init.h>
#include <cmder.h>

#include "main.h"

hal_uart_t * uart3;

int usr_root (void)
    {
    uart3 = hal_uart_open ("uart3");
        
    extern int link_main (uintptr_t);
    task_spawn ("link_main", 1, 0, 0x1000, link_main, 0);

#if 0
    HAL_GPIO_WritePin (MODULE_POWER_ON_GPIO_Port, MODULE_POWER_ON_Pin, GPIO_PIN_RESET);
    task_delay (1500);
    HAL_GPIO_WritePin (MODULE_POWER_ON_GPIO_Port, MODULE_POWER_ON_Pin, GPIO_PIN_SET);
    task_delay (5000);
    HAL_GPIO_WritePin (MODULE_POWER_ON_GPIO_Port, MODULE_POWER_ON_Pin, GPIO_PIN_RESET);
#endif
    //hal_uart_write (uart3, (const unsigned char *) "AT\r\n", 4);

    return 0;
    }

static int __cmd_at (cmder_t * cmder, int argc, char * argv [])
    {
    uint8_t ch;

    hal_uart_write (uart3, (const unsigned char *) "AT", 2);

    if (argc != 1)
        {
        hal_uart_write (uart3, (const unsigned char *) argv [1], strlen (argv [1]));
        }

    hal_uart_write (uart3, (const unsigned char *) "\r\n", 2);
        
    while (hal_uart_timedread (uart3, &ch, 1, CONFIG_SYS_TICK_HZ / 10) != 0)
    {
        kprintf ("%c", ch);
    }

    return 0;
    }

CMDER_CMD_DEF ("at", "AT command", __cmd_at);
CMDER_CMD_DEF ("AT", "AT command", __cmd_at);
